package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_bankcard;

import cn.com.api.service.ApiT_bankcardService;

import cn.com.util.*;
import cn.com.util.*;


public class T_bankcardBaseController {

	private static Logger logger = Logger.getLogger(T_bankcardBaseController.class);
	
	@Autowired
	private ApiT_bankcardService apiT_bankcardService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_bankcard-list")
	public ModelAndView t_bankcard_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_bankcard_list.jsp");
		
		List<ApiT_bankcard> t_bankcards = apiT_bankcardService.findT_bankcards();
		mv.addObject("t_bankcards", t_bankcards);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_bankcard-add-pre")
	public ModelAndView t_bankcard_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_bankcard_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_bankcard t_bankcard = apiT_bankcardService.findT_bankcardById(Integer.valueOf(id));
			mv.addObject("t_bankcard",t_bankcard);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_bankcard-add")
	public void t_bankcard_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String bankcardid = request.getParameter("bankcardid");
		String cardname = request.getParameter("cardname");
		String cardno = request.getParameter("cardno");
		String openingbank = request.getParameter("openingbank");
		String credentialstype = request.getParameter("credentialstype");
		String credentialsno = request.getParameter("credentialsno");
		String phone = request.getParameter("phone");
		String bankcardtype = request.getParameter("bankcardtype");
		String u_id = request.getParameter("u_id");
		String cteate_time = request.getParameter("cteate_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_bankcard t_bankcard = new ApiT_bankcard();
            /*
		    t_bankcard.setBankcardid(bankcardid);
		    t_bankcard.setCardname(cardname);
		    t_bankcard.setCardno(cardno);
		    t_bankcard.setOpeningbank(openingbank);
		    t_bankcard.setCredentialstype(credentialstype);
		    t_bankcard.setCredentialsno(credentialsno);
		    t_bankcard.setPhone(phone);
		    t_bankcard.setBankcardtype(bankcardtype);
		    t_bankcard.setU_id(u_id);
		    t_bankcard.setCteate_time(cteate_time);
	        */
			apiT_bankcardService.insertT_bankcard(t_bankcard);
		}
		//edit
		else{
			ApiT_bankcard t_bankcard = apiT_bankcardService.findT_bankcardById(Integer.valueOf(id));
	        /*
		    t_bankcard.setBankcardid(bankcardid);
		    t_bankcard.setCardname(cardname);
		    t_bankcard.setCardno(cardno);
		    t_bankcard.setOpeningbank(openingbank);
		    t_bankcard.setCredentialstype(credentialstype);
		    t_bankcard.setCredentialsno(credentialsno);
		    t_bankcard.setPhone(phone);
		    t_bankcard.setBankcardtype(bankcardtype);
		    t_bankcard.setU_id(u_id);
		    t_bankcard.setCteate_time(cteate_time);
			*/
			apiT_bankcardService.updateT_bankcard(t_bankcard);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_bankcard-delete")
	public void t_bankcard_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_bankcardService.deleteT_bankcard(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



